import MSData
import MSLogger

import datetime
import time
import pdb


class WorkerBase:
### CONNECTION MANIPULATORS ###
        #VIRTUAL returns a connection instance
    def getConnection(self):
        return None

        #VIRTUAL checks if a connection good
    def isConnectionGood(self):
        return False

        #VIRTUAL returns name of connection
    def getConnectionName(self):
        return None

        #connects to a worker's target
    def connect(self):
        if self.isConnectionGood():
            return True
        
        attemptCount = 0
        while True:
            try:
                self.connection = self.getConnection()
                break
            except Exception, err:
                secs = 2
                attemptCount += 1
                if attemptCount > 3:
                    attemptCount = 0
                    secs *= 10
                MSLogger.logWarn('Worker::connect: Connection to %s failed: attempt(%s) secs to sleep(%s) error: %s' % (str(self.getConnectionName()), str(attemptCount), str(secs), str(err)))
                time.sleep(secs)
                continue
        return True


### TIME MANIPULATORS ###
    def getNow(self):
        return datetime.datetime.fromtimestamp(time.time())
    def getNowPastTenMins(self):
        return self.getNow() - datetime.timedelta(0, 600)
    def getYesterday(self):
        now = self.getNow()
        return datetime.datetime(now.year, now.month, now.day, 0,0,0,0) 
    def getBeginEnd(self, LastEvent):
        begin = None
        if LastEvent != None:
            begin = LastEvent
        else:
            begin = self.getNowPastTenMins()
        end = self.getNow()
        tr = (begin.strftime(MSData.TIME_FORMAT_MAGIC), end.strftime(MSData.TIME_FORMAT_MAGIC))
        MSLogger.logDebug('WorkerBase::getBeginEnd: LastEvent(%s) TimeRange(%s-%s)' % (str(LastEvent), str(tr[0]), str(tr[1])))
        return tr


### READER STUFF ###

### PGSM MANIPULATORS ###
        #VIRTUAL reads pgsm
    def getEventsPGSM(self, MSObject):
        return None
        #VIRTUAL process pgsm
    def processEventsPGSM(self, MSObject, Events):
        return 
        #reads and processes pgsm
    def readEventsPGSM(self, MSObjects):
        statusString = 'processed: '
        #pdb.set_trace()
        for obj in MSObjects:
            try:
                events      = self.getEventsPGSM(obj)
                doneNum     = self.processEventsPGSM(obj, events)
                
                if events:
                    statusString += '%s(%s:%d)' % (str(obj.objectPCNId), str(doneNum), len(events))
            except Exception, err:
                MSLogger.logError('Worker::readEventsPGSM: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        MSLogger.logInfo('Worker::readEventsPGSM: %s' % statusString)
        return

### CONTACTID MANIPULATORS ###
    def getEventsContactId(self, MSObject):
        return None
    def processEventsContactId(self, MSObject, Events):
        return
    def readEventsContactId(self, MSObjects):
        statusString = 'processed: '
        for obj in MSObjects:
            try:
                events      = self.getEventsContactId(obj)
                doneNum     = self.processEventsContactId(obj, events)
                
                if events:
                    statusString += '%s(%s:%d)' % (str(obj.objectPCNId), str(doneNum), len(events))
            except Exception, err:
                MSLogger.logError('Worker::readEventsContactId: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        MSLogger.logInfo('Worker::readEventsContactId: %s' % statusString)
        return

### BOLID SURGUARD MANIPULATORS ###
    def getEventsBolid(self, MSObject):
        return None
    def processEventsBolid(self, MSObject, Events):
        return
    def readEventsBolid(self, MSObjects):
        statusString = 'processed: '
        for obj in MSObjects:
            try:
                events      = self.getEventsBolid(obj)
                doneNum     = self.processEventsBolid(obj, events)
                
                if events:
                    statusString += '%s(%s:%d)' % (str(obj.objectPCNId), str(doneNum), len(events))
            except Exception, err:
                MSLogger.logError('Worker::readEventsBolid: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        MSLogger.logInfo('Worker::readEventsBolid: %s' % statusString)
        return
### ARGUS SURGUARD MANIPULATORS ###
    def getEventsArgus(self, MSObject):
        return None
    def processEventsArgus(self, MSObject, Events):
        return
    def readEventsArgus(self, MSObjects):
        statusString = 'processed: '
        for obj in MSObjects:
            try:
                events      = self.getEventsArgus(obj)
                doneNum     = self.processEventsArgus(obj, events)
                
                if events:
                    statusString += '%s(%s:%d)' % (str(obj.objectPCNId), str(doneNum), len(events))
            except Exception, err:
                MSLogger.logError('Worker::readEventsArgus: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        MSLogger.logInfo('Worker::readEventsArgus: %s' % statusString)
        return

### GPS MANIPULATORS ### 
    def getEventsGPS(self, MSObject):
        return None
    def processEventsGPS(self, MSObject, Events):
        return
    def readEventsGPS(self, MSObjects):
        statusString = 'processed: '
        for obj in MSObjects:
            try:
                events      = self.getEventsGPS(obj)
                doneNum     = self.processEventsGPS(obj, events)

                if events:
                    statusString += '%s(%s:%d)' % (str(obj.objectPCNId), str(doneNum), len(events))
            except Exception, err:
                MSLogger.logError('Worker::readEventsGPS: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        MSLogger.logInfo('Worker::readEventsGPS: %s' % statusString)
        return 

### FINALYSERS ###
    def finalyze(self, MSObjects):
        for obj in MSObjects:
            try:
                self.finalyzeObject(obj)
            except Exception, err:
                MSLogger.logError('Worker::finalyze: object(%s) processing failed: %s' % (str(obj.objectPCNId), str(err)))
                continue
        return


### WRITER STUFF ###
    def writeEventsStatic(self, MSObjects):
        return
    def writeEventsMobile(self, MSObjects):
        return
